<?php
/**
 * Created by PhpStorm.
 * User: taotao
 * Date: 2019/9/23
 * Time: 11:13
 */

namespace App\Http\Controllers\WebApi\UserCenter;

use App\Http\Controllers\WebApi\BaseController;
use App\Http\Service\NewsService;
use Illuminate\Http\Request;

/**
 * 资讯相关
 * Class NewsController
 * @package App\Http\Controllers\WebApi\UserCenter
 */

class NewsController extends BaseController
{
    public $service = '';

    public function __construct()
    {
        parent::__construct();
        $this->service = new NewsService();
    }

    public function lists(Request $request)
    {
        $uid = $request->get('loginUid');
        $myNews = $this->service->getUserNews($uid);

        return $this->success($myNews);
    }

    public function detail(Request $request)
    {
        $news_id = $request->input('id',0);
        if(empty($news_id)){
            $this->error(30002);
        }

        $detailNews = $this->service->getDetailNews($news_id);

        return $this->success($detailNews);
    }

    public function add(Request $newsRequest)
    {
        $title = $newsRequest->input('title');
        $content = $newsRequest->input('content');
        $image = $newsRequest->input('thumb');
        $desc = $newsRequest->input('desc');
        if(!$title || !$content || !$image){
            $this->error(30002);
        }

        $insert = [
            'cid' => $newsRequest->get('loginUid'),
            'desc' => $desc,
            'title' => $title,
            'content' => $content,
            'thumb' => $image,
            'created_at' => time(),
            'updated_at' => time()
        ];

        if($this->service->addNews($insert)){
            return $this->success([]);
        }
        $this->error(50001);
    }

    public function edit(Request $newsRequest)
    {
        $title = $newsRequest->input('title');
        $content = $newsRequest->input('content');
        $image = $newsRequest->input('thumb');
        $desc = $newsRequest->input('desc');
        $news_id = $newsRequest->input('id');

        $newDetail = $this->service->getNews($news_id);
        if(!$newDetail){
            $this->error(30002);
        }

        $update = [
            'desc' => $desc,
            'title' => $title,
            'content' => $content,
            'thumb' => $image,
            'updated_at' => time()
        ];

        if($this->service->updateDetail($news_id,$update)){
            return $this->success([]);
        }
        $this->error(50001);
    }

}